<?php
namespace frontend\controllers;

use Yii;
use yii\data\Pagination; //分页
use frontend\base\BaseController;
use common\models\YanseModel;
use common\models\YanseTypeModel;
use common\models\YanseNovelModel;
use common\models\YanseImageModel;
use common\models\YanseImageUrlModel;
use frontend\utils\CurlUtil;

/**
 * Yanse controller
 */
class YanseController extends BaseController
{
    //545zh  四虎影库
    public function actionGetType()
    {
        $url = 'https://www.545zh.com/';
        $str = file_get_contents($url);
        $up = '/<li.*id="nav-dianshiju".*class=""><a.*href="(.*)".*target="_blank">(.*)<\/a><\/li>/Uis'; 
        preg_match_all($up, $str, $arr);
        $res = [];
        if(!empty($arr)){
            unset($arr[0]);
            $num = count($arr[1]);
            foreach ($arr as $key => $value) {
                foreach ($value as $k => $val) {
                    if($key == 1){
                        $name = 'url';
                    }elseif($key == 2){
                        $name = 'title';
                    }
                    $res[$k][$name] = trim(str_replace('"','',$val));
                }
            }
        }
        if(!empty($res)){
            foreach ($res as $k => $val) {
                $data[] = ['title' => $val['title'], 'source' => 1, 'url' => $url.$val['url'],'time'=>time()];
            }
        }
        $model = YanseTypeModel::tableName();
        $attr = ['title', 'source', 'url','time'];
        $flag = Yii::$app->yanse->createCommand()->batchInsert($model, $attr, $data)->execute();
        echo $flag;
    }

    public function actionGetSubType()
    {
        $url = 'https://www.545zh.com/';
        $type_list = YanseTypeModel::find()->select(['id','url','title','page'])->where(['source'=>1,'parent'=>0])->asArray()->all();
        if(!empty($type_list)){
            foreach ($type_list as $type_val) {
                $str = file_get_contents($type_val['url']);
                $up = '/<dd.*class="clearfix">(.*)<\/dd>/Uis'; 
                preg_match_all($up, $str, $a);
                $up = '/<a.*href="(.*)">(.*)<\/a>/Uis'; 
                preg_match_all($up, implode('',$a[1]), $arr);
                $data = $res = [];
                if(!empty($arr)){
                    unset($arr[0]);
                    $num = count($arr[1]);
                    foreach ($arr as $key => $value) {
                        foreach ($value as $y => $val) {
                            if($key == 1){
                                $name = 'url';
                            }elseif($key == 2){
                                $name = 'title';
                            }
                            $res[$y][$name] = trim(str_replace('"','',$val));
                        }
                    }
                }
                if(!empty($res)){
                    foreach ($res as $k => $val) {
                        $data[] = ['title' => $val['title'], 'source' => 1, 'url' => $url.$val['url'],'time'=>time(),'parent'=>$type_val['id']];
                    }
                }
                $model = YanseTypeModel::tableName();
                $attr = ['title', 'source', 'url','time','parent'];
                $flag = Yii::$app->yanse->createCommand()->batchInsert($model, $attr, $data)->execute();
                echo $flag;
            }
        }
    }

    //视频资源
    public function actionGetYanseVideo()
    {
        ini_set("max_execution_time", "72000");
        ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)');
        $url = 'https://www.545zh.com';
        $type_list =YanseTypeModel::find()->select(['id','url','title','page'])->where(['source'=>1,'type'=>1])->andWhere(['!=','parent','0'])->asArray()->all();
        if(!empty($type_list)){
            foreach ($type_list as $k => $type_val) {
                for ($i=$type_val['page'];;$i++){
                    if($i == 1){
                        $post_url = $type_val['url'];
                    }else{
                        $post_url = $type_val['url'].$i.'.html';
                    }
                    $str = CurlUtil::getInstance()->post($post_url);
                    if(!$str){
                        break;
                    }
                    $up = '/<a.*class="video-pic.*loading".*data-original="(.*)".*href=".*".*title=".*" >.*<div.*class="title"><h5.*class="text-overflow"><a.*href="(.*)".*title=".*">(.*)<\/a><\/h5><\/div>.*<div.*class="subtitle.*text-time text-overflow.*hidden-xs">(.*)<\/div>/Uis'; 
                    preg_match_all($up, $str, $a);
                    $data = $res = [];
                    if(!empty($a[0])){
                        unset($a[0]);
                        foreach ($a as $key => $value) {
                            foreach ($value as $num => $v) {
                                if($key == 1){
                                    $name = 'image';
                                }elseif($key == 2){
                                    $name = 'url';
                                    $v = $url.$v;
                                }elseif($key == 3){
                                    $name = 'title';
                                }elseif($key == 4){
                                    $name = 'time';
                                }
                                $res[$num][$name] = trim(str_replace('"','',$v));
                            }
                        }
                        if(!empty($res)){foreach ($res as $key => $value) {
                            $str = CurlUtil::getInstance()->get($value['url']);
                            $par = '/<meta.*name="description".*content="(.*)">.*httpurl.*=.*"(.*)";/Uis';
                            preg_match_all($par, $str, $a);
                            $res[$key]['url'] = $a[2][0];
                            $res[$key]['desc'] = $a[1][0];
                        }}
                        if(!empty($res)){
                            foreach ($res as $x => $y) {
                                $count = YanseModel::find()->where(['title' => $y['title'],'source'=>1,'type'=>$type_val['id']])->count();
                                if($count == 0) {
                                    $data[] = ['title'=>$y['title'],'source'=>1,'url'=>$y['url'],'author'=>'四虎影库','type'=>$type_val['id'],'image' => $y['image'],'time'=>$y['time'],'desc'=>$y['desc']];
                                }
                            }
                            if(!empty($data)){
                                $model = YanseModel::tableName();
                                $attr = ['title', 'source', 'url','author','type','image','time','desc'];
                                YanseTypeModel::updateAll(['page'=>$i+1],['id'=>$type_val['id']]);
                                $status = Yii::$app->yanse->createCommand()->batchInsert($model, $attr, $data)->execute();
                            }
                        }
                    }else{
                        break;
                    }
                }
                continue;
            }
        }
    }

    //图片资源
    public function actionGetYanseImage()
    {
        ini_set("max_execution_time", "72000");
        ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)');
        $url = 'https://www.545zh.com';
        $type_val = YanseTypeModel::find()->select(['url','page'])->where(['id'=>78])->asArray()->one();
        for ($i = $type_val['page'];;$i++){
            if($i == 1){
                $post_url = $type_val['url'];
            }else{
                $post_url = $type_val['url'].$i.'.html';
            }
            $str = CurlUtil::getInstance()->post($post_url);
            if(!$str){
                break;
            }
            $up = '/<li.*class="col-md-3.*col-sm-6.*col-xs-12.*"><a.*class="video-pic.*loading".*href="(.*)" title="(.*)".*<\/li>/Uis';
            preg_match_all($up, $str, $a);
            $res = [];
            if(!empty($a[0])){
                unset($a[0]);
                foreach ($a as $key => $value) {
                    foreach ($value as $num => $v) {
                        if($key == 1){
                            $name = 'url';
                            $v = $url.$v;
                        }elseif($key == 2){
                            $name = 'title';
                        }
                        $res[$num][$name] = trim(str_replace('"','',$v));
                    }
                }
                if(!empty($res)){foreach ($res as $key => $value) {
                    $str = CurlUtil::getInstance()->get($value['url']);
                    $par = '/<img.*src="(.*)"\/><br>/Uis';
                    preg_match_all($par, $str, $a);
                    $res[$key]['url'] = $a[1];
                }}
                if(!empty($res)){
                    foreach ($res as $y) {
                        $query = $data = [];
                        $tr = Yii::$app->yanse->beginTransaction();
                        $count = YanseImageModel::find()->where(['title' => $y['title'],'source'=>1,'type'=>78])->count();
                        if($count == 0){
                            $image_model = new YanseImageModel();
                            // echo '<pre>';
                            // print_r($image_model);die;
                            $query['desc'] = $y['title'];
                            $query['title'] = $y['title'];
                            $query['source'] = 1;
                            $query['type'] = 78;
                            $query['time'] = date('Y-m-d',time());
                            if(!$image_model->save(true,$query)){
                                $tr->rollBack();
                            }
                            $ks = YanseImageModel::updateAll($query,['id'=>$image_model->id]);
                            if($ks == 0){
                                $tr->rollBack();
                            }
                            if(!empty($y['url'])){
                                foreach ($y['url'] as $k => $x) {
                                    $data[] = ['url'=>$x,'image_id'=>$image_model->id];
                                }
                            }
                        }
                        if(!empty($data)){
                            $model = YanseImageUrlModel::tableName();
                            $attr = ['url', 'image_id'];
                            YanseTypeModel::updateAll(['page'=>$i+1],['id'=>78]);
                            $status = Yii::$app->yanse->createCommand()->batchInsert($model, $attr, $data)->execute();
                            if($status <= 0){
                                $tr->rollBack();
                            }
                        }
                        $tr->commit();
                    }
                }
            }else{
                break;
            }
        }
    }

    //小说资源
    public function actionGetYanseNovel()
    {
        ini_set("max_execution_time", "72000");
        ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)');
        $url = 'https://www.545zh.com';
        $type_list =YanseTypeModel::find()->select(['id','url','title','page'])->where(['source'=>1,'type'=>2])->andWhere(['!=','parent','0'])->asArray()->all();
        if(!empty($type_list)){
            foreach ($type_list as $k => $type_val) {
                for ($i=$type_val['page'];;$i++){
                    if($i == 1){
                        $post_url = $type_val['url'];
                    }else{
                        $post_url = $type_val['url'].$i.'.html';
                    }
                    $str = CurlUtil::getInstance()->post($post_url);
                    if(!$str){
                        break;
                    }
                    $up = '/<li class="col-md-14 col-sm-16 col-xs-12 clearfix news-box"><a href="(.*)" title="(.*)"><span class="xslist text-bg-c">.*<\/span><\/a><\/li>/Uis'; 
                    preg_match_all($up, $str, $a);
                    $data = $res = [];
                    if(!empty($a[0])){
                        unset($a[0]);
                        foreach ($a as $key => $value) {
                            foreach ($value as $num => $v) {
                                if($key == 1){
                                    $name = 'url';
                                    $v = $url.$v;
                                }elseif($key == 2){
                                    $name = 'title';
                                }
                                $res[$num][$name] = trim(str_replace('"','',$v));
                            }
                        }
                        if(!empty($res)){foreach ($res as $key => $value) {
                            $str = CurlUtil::getInstance()->get($value['url']);
                            $par = '/<div class="news_top text-center text-overflow"><span>时间：(.*)<\/span>.*<\/div>.*<div class="xs-details-content text-xs">.*<p>(.*)<br \/>.*<\/p>.*<\/div>/Uis';
                            preg_match_all($par, $str, $a);
                            if(!$a[1]){
                                unset($res[$key]);
                                continue;
                            }
                            $res[$key]['time'] = $a[1][0];
                            $res[$key]['content'] = $a[2][0];
                        }}
                        if(!empty($res)){
                            foreach ($res as $x => $y) {
                                $count = YanseNovelModel::find()->where(['title' => $y['title'],'source'=>1,'type'=>$type_val['id']])->count();
                                if($count == 0) {
                                    $data[] = ['title'=>$y['title'],'source'=>1,'author'=>'四虎影库','type'=>$type_val['id'],'time' => $y['time'],'content'=>$y['content']];
                                }
                            }
                            if(!empty($data)){
                                $model = YanseNovelModel::tableName();
                                $attr = ['title', 'source','author','type','time','content'];
                                YanseTypeModel::updateAll(['page'=>$i+1],['id'=>$type_val['id']]);
                                $status = Yii::$app->yanse->createCommand()->batchInsert($model, $attr, $data)->execute();
                            }
                        }
                    }else{
                        break;
                    }
                }
                continue;
            }
        }
    }

}